<?php

namespace app\backend\service;


use app\backend\model\ZhMatchmakerCharge;
use app\backend\model\ZhMatchmakerLevel;
use think\exception\ValidateException;
use xhadmin\CommonService;

class ZhMatchmakerChargeService extends CommonService
{

    /*
     * @Description  列表数据
     */
    public static function indexList($where, $limit, $page)
    {
        try {
            $res = ZhMatchmakerCharge::where($where)->order(['displayorder' => 'desc'])->paginate(['list_rows' => $limit, 'page' => $page])->toArray();
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return ['rows' => $res['data'], 'total' => $res['total']];
    }

    public static function byId($id)
    {
        try {
            $res = ZhMatchmakerCharge::where(['id'=>$id])->find();
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return $res;
    }

    /*
     * @Description  修改
     */
    public static function update($data)
    {
        try {
            validate(\app\backend\validate\ZhMatchmakerCharge::class)->scene('update')->check($data);
            $data['createtime'] = strtotime($data['createtime']);
            $res                = ZhMatchmakerCharge::update($data);
        } catch (ValidateException $e) {
            throw new ValidateException ($e->getError());
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        if (!$res) {
            throw new ValidateException ('操作失败');
        }
        return $res;
    }

    //把数据库查询出来的某一行数据转换为json对象
    public static function convertToJosn($v1){
        //解码json
        $data_str = html_entity_decode($v1["matchmaker_money_json"]);
        $ow = [];
        $row["id"] = $v1["id"];
        $row["entry_name"] = $v1["entry_name"];
        //格式化为json对象
        $row["matchmaker_money_json"] = json_decode($data_str);
        $row["comment"] = $v1["comment"];
        $row["is_fixed_price"] = $v1["is_fixed_price"];
        $row["createtime"] =  date('Y-m-d H:i:s', strtotime('@' . $v1["createtime"]));
        return $row;
    }

}